package com.fenxiang.tieba;

import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

/**
 * Created by lixiang on 09/07/2017.
 */
public class test {

    public static void main(String[] args) {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        try {
            // 创建httpget.
            HttpGet httpget = new HttpGet("http://tieba.baidu.com/dc/common/imgtbs");
            System.out.println("executing request " + httpget.getURI());

            httpget.setHeader("cookie","FP_UID=8b3bbfdfee0122bc82874802232555db; TIEBA_USERTYPE=c5f54a971f23185f29161d67; bdshare_firstime=1498902608213; PSTM=1498918997; BIDUPSID=A005E92A1BDB2CEA31BF2998146ABFD9; BDUSS=NBVUdLU1pTVXVpUFlvR2E4eE82R2FHMUdubjhPLUNwM2pzbGRJZG9YWFF3bjlaSUFBQUFBJCQAAAAAAAAAAAEAAADvNnUwzqLQprXE0KHQobW2AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAANA1WFnQNVhZbz; BAIDUID=35158332E768586A1D8744350387D1BA:FG=1; TIEBAUID=ab202a74762b392e64dafb99; STOKEN=910d429df842194a9e15151a67d36e61c487c3b6375df56799117b4f25edea62; fixed_bar=1; Hm_lvt_287705c8d9e2073d13275b18dbd746dc=1498833623,1498865363,1498902609,1498955886; Hm_lpvt_287705c8d9e2073d13275b18dbd746dc=1498955886; bottleBubble=1; showCardBeforeSign=1; rpln_guide=1; BDRCVFR[feWj1Vr5u3D]=I67x6TjHwwYf0; 812988143_FRSVideoUploadTip=1; PSINO=1; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; H_PS_PSSID=23199_1449_21106_18559_23633_22158; FP_LASTTIME=1498992451494; wise_device=0");
            // 执行get请求.
            CloseableHttpResponse response = httpclient.execute(httpget);
            try {
                // 获取响应实体
                HttpEntity entity = response.getEntity();
                System.out.println("--------------------------------------");
                // 打印响应状态
                System.out.println(response.getStatusLine());
                if (entity != null) {
                    // 打印响应内容长度
                    System.out.println("Response content length: " + entity.getContentLength());
                    // 打印响应内容
                    System.out.println("Response content: " + EntityUtils.toString(entity));
                }
                System.out.println("------------------------------------");
            } finally {
                response.close();
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭连接,释放资源
            try {
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
